package stz.flash.main.flashlight.field;

import net.rim.device.api.system.Bitmap;
import net.rim.device.api.ui.component.SeparatorField;
import stz.flash.Info;
import stz.flash.main.FlashTimer;
import stz.flash.main.ImageXY;
import stz.flash.option.flashlight.BacklightOption;
import blackberry.util.field.DjNonHighlightBitmapField;
import blackberry.util.image.DjImageResizer;


public class BacklightField extends HighlightManagerForFlashMode {

	private static final String FILE_NAME_OF_BACKLIGHT = "BACKLIGHT.png";
	
	public BacklightField() {
		super(Info.COLOR_OF_FOCUS,
				Info.COLOR_OF_SELECT,
				Info.GLOBAL_ALPHA_OF_HIGHLIGHT,
				null);
		add(new DjNonHighlightBitmapField(
				DjImageResizer.fitIntoDisplay(
						Bitmap.getBitmapResource(FILE_NAME_OF_BACKLIGHT),
						ImageXY.WIDTH_OF_TEXT_IMAGE, 
						ImageXY.HEIGHT_OF_TEXT_IMAGE, 
						ImageXY.WIDTH_OF_VIRTUAL_IMAGE, 
						ImageXY.HEIGHT_OF_VIRTUAL_IMAGE, 
						0, Bitmap.FILTER_BILINEAR), 
				FOCUSABLE));
	}

	public void execute() {
		toggleBacklight();
	}
	
	public static void toggleBacklight(){
		setLatestBacklight(!isLatestBacklight());
		FlashTimer.getIns().stopAndStart(FlashTimer.STOP_AND_START_backlight_toggle);
	}

	private static void setLatestBacklight(boolean on) {
		BacklightOption.getIns().setLatestBacklightStateAndNotify(on);
	}

	private static boolean isLatestBacklight() {
		return BacklightOption.getIns().isLatestBacklightState();
	}
	
	protected void onFocus(int i) {
		super.onFocus(i);
	}
}
